home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / commands / uue.c < prev    next >
C/C++ Source or Header  |  1990-07-23  |  4KB  |  197 lines

  1. /* uue - bulletproof version of uuencode */
  2.  
  3. /* Uue -- encode a file so that it's printable ascii, short lines
  4.  *
  5.  * Slightly modified from a version posted to net.sources a while back,
  6.  * and suitable for compilation on the IBM PC
  7.  *
  8.  * modified for Lattice C on the ST - 11.05.85 by MSD
  9.  * modified for ALCYON on the ST -    10-24-86 by RDR
  10.  * modified a little more for MWC...  02/09/87 by JPHD
  11.  * (An optional first argument of the form: -nnumber (e.g. -500), will
  12.  * produce a serie of files that long, linked by the include statement,
  13.  * such files are automatically uudecoded by the companion program.)
  14.  * More mods, - ...           05/06/87 by jphd
  15.  * Mods for TOPS 20, and more.     08/06/87 by jphd
  16.  *     (remove freopen and rindex...change filename generation...)
  17.  * (A lot more to do about I/O speed, avoiding completely the stdio.h...)
  18.  *
  19.  */
  20.  
  21.  
  22. #include <ctype.h>
  23. #include <stdio.h>
  24.  
  25. #define USAGE
  26. #define FILE_NAME 10        /* affects how long names are truncated */
  27.  
  28. /* ENC is the basic 1 character encoding function to make a char printing */
  29. #define ENC(c) (((c) & 077) + ' ')
  30.  
  31. extern FILE *fopen();
  32. FILE *fp, *outp;
  33. char ofname[80];
  34. int lenofname;
  35. int stdo = 0;
  36.  
  37. #ifdef ST
  38. #define READ "rb"
  39. #else
  40. #define READ "r"
  41. #endif
  42.  
  43. int part = 'a', chap = 'a';
  44. #define SEQMAX 'z'
  45. #define SEQMIN 'a'
  46. char seqc = SEQMAX;
  47.  
  48. int split = 0;
  49. int fileln = 32000;
  50.  
  51. main(argc, argv)
  52. int argc;
  53. char *argv[];
  54. {
  55.   char *fname;
  56.  
  57.   if (argc < 2) {
  58.     fprintf(stderr, "Usage: uue [-n] inputfile [-]\n");
  59.     exit(2);
  60.   }
  61.   if (argv[1][0] == '-') {
  62.     fileln = -atoi(argv[1]);
  63.     if (fileln <= 0) {
  64.         fprintf(stderr, "Wrong file length arg.\n");
  65.         exit();
  66.     }
  67.     split = 1;
  68.     argv++;
  69.     argc--;
  70.   }
  71.   if ((fp = fopen(argv[1], READ)) == NULL) {    /* binary input !!! */
  72.     fprintf(stderr, "Cannot open %s\n", argv[1]);
  73.     exit(1);
  74.   }
  75.   strcpy(ofname, argv[1]);
  76.   fname = ofname;
  77.   do {
  78.     if (*fname == '.') *fname = '\0';
  79.   } while (*fname++);
  80.   /* 10 char prefix + .uue -> 14 chars MAX */
  81.   lenofname = strlen(ofname);
  82.   if (lenofname > FILE_NAME) ofname[FILE_NAME] = '\0';
  83.   strcat(ofname, ".uue");
  84.   lenofname = strlen(ofname);
  85.   if (!split && (argc > 2) && (argv[2][0] == '-')) {
  86.     stdo = 1;
  87.     outp = stdout;
  88.   } else {
  89.     makename();
  90.     if ((outp = fopen(ofname, "w")) == NULL) {
  91.         fprintf(stderr, "Cannot open %s\n", ofname);
  92.         exit(1);
  93.     }
  94.   }
  95.   maketable();
  96.   fprintf(outp, "begin %o %s\n", 0644, argv[1]);
  97.   encode();
  98.   fprintf(outp, "end\n");
  99.   fclose(outp);
  100.   exit(0);
  101. }
  102.  
  103. /* Create ASCII table so a mailer can screw it up and the decode
  104.  * program can restore the error.
  105.  */
  106. maketable()
  107. {
  108.   register int i, j;
  109.  
  110.   fputs("table\n", outp);
  111.   for (i = ' ', j = 0; i < '`'; j++) {
  112.     if (j == 32) putc('\n', outp);
  113.     fputc(i++, outp);
  114.   }
  115.   putc('\n', outp);
  116. }
  117.  
  118. /* Generate the names needed for single and multiple part encoding.  */
  119. makename()
  120. {
  121.   if (split) {
  122.     ofname[lenofname - 1] = part;
  123.     ofname[lenofname - 2] = chap;
  124.   }
  125. }
  126.  
  127. /* Copy from in to out, encoding as you go along.  */
  128. encode()
  129. {
  130.   char buf[80];
  131.   register int i, n;
  132.   register int lines;
  133.   lines = 6;
  134.  
  135.   for (;;) {
  136.     n = fr(buf, 45);
  137.     putc(ENC(n), outp);
  138.     for (i = 0; i < n; i += 3) outdec(&buf[i]);
  139.     putc(seqc, outp);
  140.     seqc--;
  141.     if (seqc < SEQMIN) seqc = SEQMAX;
  142.     putc('\n', outp);
  143.     ++lines;
  144.     if (split && (lines > fileln)) {
  145.         part++;
  146.         if (part > 'z') {
  147.             part = 'a';
  148.             if (chap == 'z')
  149.                 chap = 'a';    /* loop ... */
  150.             else
  151.                 chap++;
  152.         }
  153.         makename();
  154.         fprintf(outp, "include %s\n", ofname);
  155.         fclose(outp);
  156.         if ((outp = fopen(ofname, "w")) == NULL) {
  157.             fprintf(stderr, "Cannot open %s\n", ofname);
  158.             exit(1);
  159.         }
  160.         maketable();
  161.         fprintf(outp, "begin part %c %s\n", part, ofname);
  162.         lines = 6;
  163.     }
  164.     if (n <= 0) break;
  165.   }
  166. }
  167.  
  168. /* Output one group of 3 bytes, pointed at by p, on file f.  */
  169. outdec(p)
  170. register char *p;
  171. {
  172.   register int c1, c2, c3, c4;
  173.  
  174.   c1 = *p >> 2;
  175.   c2 = (*p << 4) & 060 | (p[1] >> 4) & 017;
  176.   c3 = (p[1] << 2) & 074 | (p[2] >> 6) & 03;
  177.   c4 = p[2] & 077;
  178.   putc(ENC(c1), outp);
  179.   putc(ENC(c2), outp);
  180.   putc(ENC(c3), outp);
  181.   putc(ENC(c4), outp);
  182. }
  183.  
  184. /* Fr: like read but stdio */
  185. int fr(buf, cnt)
  186. register char *buf;
  187. register int cnt;
  188. {
  189.   register int c, i;
  190.   for (i = 0; i < cnt; i++) {
  191.     c = fgetc(fp);
  192.     if (feof(fp)) return(i);
  193.     buf[i] = c;
  194.   }
  195.   return(cnt);
  196. }
  197.